﻿using Csb.SignalRClient.Model;
using Microsoft.AspNetCore.SignalR.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace Csb.SignalRClient
{
    public partial class LoginView : Form
    {
        private HubConnection _hubConnection;
        private User User { get; set; }
        public LoginView()
        {
            InitializeComponent();
        }

        private async void login_Click(object sender, EventArgs e)
        {
            //客户端调用服务器更新数据

            if (string.IsNullOrEmpty(this.userName.Text.Trim()) || string.IsNullOrEmpty(this.password.Text.Trim()))
            {
                MessageBox.Show("用户名或密码不能为空！");
                return;
            }
            User = new User();
            User.UserName = this.userName.Text.Trim();
            User.UserPassord = this.password.Text.Trim();

            ////调用服务端，ChatHub类中的方法UpdateMessage，接收客户端发送的更新数据
            //await _hubConnection.InvokeAsync("Login", User);

            using (HttpClient client = new HttpClient())
            {
                var json = System.Text.Json.JsonSerializer.Serialize(User);
                var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
                var response = await client.PostAsync("http://127.0.0.1:8081/Account/Login", content);

                if (response.IsSuccessStatusCode)
                {
                    Form1 mainForm = new Form1(User);
                    mainForm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("登录失败！");
                }
            }

        }

        private void LoginView_Load(object sender, EventArgs e)
        {
            //_hubConnection = new HubConnectionBuilder()
            //  .WithUrl("http://localhost:8081/signalchat") //指定服务器的hub端点地址
            //  .WithAutomaticReconnect() //支持重连，可自定义配置
            //  .Build();
            //User = new User();
            //// 订阅服务器端发送的消息，绑定客户端方法，指定名字，不然服务器不知道调用谁
            //_hubConnection.On<MsgInfo>("ReceiveLoginMsg", (msg) =>
            //{
            //    //// 处理接收到的消息
            //    // 确保在 UI 线程中更新 TextBox
            //    //this.Invoke(new Action(() =>
            //    //{


            //    //}));

            //    //User user = JsonConvert.DeserializeObject<User>(msg.user);

            //    //Form1 form1 = new Form1();
            //    //form1.ShowDialog();


            //});

            //// 启动连接
            //_hubConnection.StartAsync().ContinueWith(task =>
            //{
            //    if (task.IsFaulted)
            //    {
            //        MessageBox.Show($"Error connecting to SignalR Hub: {task.Exception?.InnerExceptions[0]}");
            //    }
            //    else
            //    {
            //        //MessageBox.Show("Connected to SignalR Hub");
            //        User.Id = _hubConnection.ConnectionId;
            //    }
            //});

        }
    }
}
